<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class Bahan extends Member_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('tgl_indonesia'));
		$this->load->model('data_gudang_model','',TRUE);
	}
	
	function index()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="bahan/bahan";
		$data['data'] = $this->data_gudang_model->getAll()->result();
		$this->template->load('index',$partial,$data);
	}
	function add()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="bahan/form_bahan";
		$get_id = "";
		$data['satuan'] = $this->data_gudang_model->getSatuan($get_id)->result();
		$data['kategori'] = $this->data_gudang_model->getKategori($get_id)->result();
		$data['readonly']="";
		$data['data']['id_bahan']='';
		$data['data']['nama_bahan']='';
		$data['data']['harga']='';
		$data['data']['stok']='';
		$data['data']['id_satuan']='';
		$data['data']['id_kategori_bahan']='';
		$this->template->load('index',$partial,$data);
	}
	
	function update($id)
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="bahan/form_bahan";
		$data['data'] = $this->data_gudang_model->get_by_id($id)->row_array();
		$get_id = $this->data_gudang_model->get_by_id($id)->row();
		$data['satuan'] = $this->data_gudang_model->getSatuan($get_id->id_satuan)->result();
		$data['kategori'] = $this->data_gudang_model->getKategori($get_id->id_kategori_bahan)->result();
		$data['readonly']="readonly";
		$this->template->load('index',$partial,$data);
	}
	
	function delete($id)
	{
		$this->data_gudang_model->delete($id);
		redirect('bahan');
	}
	function bahan_masuk()
	{
		//tambah bahan masuk
		if (isset($_POST['add'])){
			$last = $this->data_gudang_model->get_last()->row();
			if(isset($last->no_transaksi))
				$last = $last->no_transaksi+1;
			else
				$last = 1;
				
			$id_bahan = $this->data_gudang_model->get_id_bahan($this->input->post('nama_bahan'))->row();
			$bahan_masuk=array(
					'no_transaksi'=>$last,
					'id_bahan'=>$id_bahan->id_bahan,
					'tanggal'=>$this->input->post('tanggal'),
					'jumlah'=>$this->input->post('jumlah')
				);
			$jumlah_by_id = $this->data_gudang_model->getTemp_jumlah_by_id($id_bahan->id_bahan)->row();
			if(isset($jumlah_by_id->jumlah))
				$jumlah = $jumlah_by_id->jumlah;
			else
				$jumlah = 0;
			
			$this->data_gudang_model->saveTemp_bahan_masuk($bahan_masuk,$jumlah);

		}
		
		//update bahan masuk
		if (isset($_POST['save'])){
			$id_bahan = $this->input->post('id');
			$bahan_masuk=array(
			'tanggal'=>$this->input->post('tanggal'),
			'jumlah'=>$this->input->post('jumlah')
				);
			$this->data_gudang_model->updateTemp_bahan_masuk($id_bahan, $bahan_masuk);
			}
		
		//delete bahan masuk	
		if (isset($_POST['delete'])) {
			$this->data_gudang_model->deleteTemp_bahan_masuk($this->input->post('id'));
		}
		
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="bahan/form_bahan_masuk";
		$data['nama_bahan_gudang']=$this->data_gudang_model->getName()->result();
		$data['temp']=$this->data_gudang_model->getTemp_bahan_masuk()->result();
		$data['readonly']="";
		$this->template->load('index',$partial,$data);
	}
	function action()
	{
	
		if (isset($_POST['add'])){
			$bahan=array(
			'id_bahan'=>$this->input->post('id_bahan'),
			'nama_bahan'=>$this->input->post('nama_bahan'),
			'stok'=>$this->input->post('stok'),
			'harga'=>$this->input->post('harga'),
			'id_satuan'=>$this->input->post('satuan'),
			'id_kategori_bahan'=>$this->input->post('kategori'));
			$this->data_gudang_model->save($bahan);
			}
		else if (isset($_POST['update'])){
		$id = $this->input->post('id_bahan');
		$bahan=array(
			'nama_bahan'=>$this->input->post('nama_bahan'),
			'stok'=>$this->input->post('stok'),
			'harga'=>$this->input->post('harga'),
			'id_satuan'=>$this->input->post('satuan'),
			'id_kategori_bahan'=>$this->input->post('kategori'));
			$this->data_gudang_model->update($id,$bahan);
		}
		redirect('bahan');
	}
	function report(){
			//pindahkan dari temp	
			$this->data_gudang_model->save_bahan_masuk();
			$update_stok=$this->data_gudang_model->total_stok();
			foreach($update_stok->result() as $row){
				
				$this->data_gudang_model->update($row->id_bahan, array('stok'=>$row->total));
			}
			$this->data_gudang_model->truncateTemp_bahan_masuk();
			$this->export();
//		redirect('bahan/bahan_masuk');	
		}
	function export(){
		$this->load->library('cezpdf');
		$this->load->helper('pdf');
		 
		prep_pdf(); // creates the footer for the document we are creating.
		$no = 1;
		$data = $this->data_gudang_model->get_bahan_masuk_by_no();
		foreach($data->result() as $row){
			$db_data[] = array('no' => $no++, 'nama_bahan' => $row->nama_bahan, 'jumlah' => $row->jumlah);
		}

		$col_names = array(
			'no' => 'Number',
			'nama_bahan' => 'Nama Bahan',
			'jumlah' => 'Jumlah'
		);

		$this->cezpdf->ezTable($db_data, $col_names, 'Contact List', array('width'=>500, 'height'=>300));
		$this->cezpdf->ezStream();
	}
	function cek_not_name(){
		$name = $this->input->post('nama_bahan');
		if(!empty($name)){
			$check=$this->data_gudang_model->cek_name($name);
			if($check->num_rows == 0)
				echo "false";
			else
				echo "true";
		}
		else
			echo "true";
	}
}